import { reactive } from "vue";
import { defineStore } from "pinia";

export const useUserStore = defineStore("user", () => {
  const user = reactive({
    userInfo: window.localStorage.getItem("userInfo")
      ? JSON.parse(window.localStorage.getItem("userInfo"))
      : {},
    lang: window.localStorage.getItem("language") || "zh_CN",
    theme: window.localStorage.getItem("theme") || "standard",
  });

  function getUserInfo() {
    return user.userInfo;
  }

  function setUserInfo(data) {
    window.localStorage.setItem("userInfo", JSON.stringify(data));
    user.userInfo = data;
  }

  function setLanguage(data) {
    window.localStorage.setItem("language", data);
    user.lang = data;
  }

  function setTheme(data) {
    window.localStorage.setItem("theme", data);
    user.theme = data;
  }

  return { user, setUserInfo, setLanguage, setTheme, getUserInfo };
});
